/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "Object.h"

namespace PQEngine
{
	ObjectVector Object::_objects=ObjectVector();
	Object::Object(ObjectType type)
	{
		_objectType=type;
		_id=_objects.size();
		_objects.push_back(this);


		for(int i=0;i<Event::Event_Type_Count;i++){
			_handleEvents[i]=false;//at first all object didn't handle any event
		}
		
	}

	Object::~Object()
	{
		_objects[_id]=0;
		
	}


	void Object::dispatchEvent(Event* evt)
	{
		if(!evt||!evt->sender()||evt->isHandled()){
			return;
		}
		//tranverse all objects that can handle this event
		int size=_objects.size();
		for(int i=0;i<size;i++){
			if(_objects[i]){
				if(!evt||evt->isHandled()){
					return;
				}

				if(_objects[i]->isEventAccepted(evt->type())){
					_objects[i]->handleEvent(evt);//maybe change evt 
				}
			}
		}
	}

	
	Object* Object::getObject(int id)
	{
		int size=_objects.size();
		if(id>=0&&id<size){
			return _objects[id];
		}else{
			return 0;
		}
	}

	void Object::removeObject(int id)
	{
		Object* object=getObject(id);
		if(object){
			_objects[id]=0;//free the slot
			delete object;
		}
	}

	int Object::getId()
	{
		return _id;
	}

	int Object::getObjectCount()
	{
		return _objects.size();
	}
}
